package com.agency8x.model;

import java.util.List;

import com.agency8x.model.base.BaseAuthRoleModel;
import com.agency8x.sqls.AuthRoleSqls;
import com.agency8x.util.RegexUtil;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;

public class AuthRoleModel extends BaseAuthRoleModel<AuthRoleModel> {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public static final AuthRoleModel me = new AuthRoleModel().dao();
	
	/**
	 * 所有 sql 与业务逻辑写在 Service 中，在此放在 Model 中仅为示例
	 */
	public Page<AuthRoleModel> paginate(int pageNumber, int pageSize, String where) {
		String sql =AuthRoleSqls.me.getPagingSelectSql();
		String sqlFrom = AuthRoleSqls.me.getPagingExceptSelectSql();
		if(where != null && !"".equals(where.trim())){
			if(RegexUtil.isNumeric(where))
				sqlFrom = sqlFrom + " where id = " + where;
		}
		sqlFrom+=" order by sort, id";
		return paginate(pageNumber, pageSize, sql,sqlFrom);
	}

	public boolean saveRules(Integer id, String rules) {
		int count=Db.update(AuthRoleSqls.me.getUpdateRulesSql(),rules,id);
		return count>0;
	}

	public List<AuthRoleModel> getRoles() {
		String sqlstmt=AuthRoleSqls.me.getSelectSql();
		return find(sqlstmt);
	}

	public boolean changeStatus(Integer id, Integer status) {
		String sqlstmt = AuthRoleSqls.me.getUpdateStatusSql();
		int count = Db.update(sqlstmt, status, id);
		return count > 0;
	}

	public boolean changeSort(Integer id, Integer sort) {
		String sqlstmt = AuthRoleSqls.me.getUpdateSortSql();
		int count = Db.update(sqlstmt, sort, id);
		return count > 0;
	}
}

